package com.demo.service.impl;

import com.demo.dto.SysUserDTO;
import com.demo.dto.UserDTO;
import com.demo.entity.SysUser;
import com.demo.mapper.SysUserMapper;
import com.demo.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper mapper;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;



    @Override
    public SysUser getByLoginName(String loginName) {
        return mapper.getByLoginname(loginName);
    }

    @Override
    public SysUser getByUserId(String userId) {
        return mapper.getByUserId(userId);
    }

    @Override
    public int add(SysUser user) {
        user.setLoginPass(passwordEncoder.encode(user.getLoginPass()));
        return mapper.insert(user);
    }

    @Override
    public int password(String loginName, String loginPass) {
        String pass = passwordEncoder.encode(loginPass);
        return mapper.password(loginName, pass);
    }


    @Override
    public PageInfo<SysUser> list(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<SysUser> sysUsers = mapper.list();
        return new PageInfo<>(sysUsers);
    }

    @Override
    public PageInfo<SysUserDTO> listByRole(int pageIndex, int pageSize, String roleId,String userNumber) {
        PageHelper.startPage(pageIndex, pageSize);
        List<SysUserDTO> sysUsers = mapper.listByRole(roleId,userNumber);
        return new PageInfo<>(sysUsers);

    }

    @Override
    public List<SysUserDTO> listDepUser(String roleId,String telephone) {
        List<SysUserDTO> sysUsers = mapper.listByRole(roleId,telephone);
        return sysUsers;

    }


    @Override
    public int update(SysUser user) {
        return mapper.update(user);
    }

    @Override
    public int remove(String loginName) {
        return mapper.remove(loginName);
    }

    @Override
    public int cleanUser(String loginName) {
        return mapper.cleanUser(loginName);
    }



    @Override
    public int confirmLoginName(String loginName, String telephone, String userNumber) {
        return mapper.confirmLoginName(loginName, telephone, userNumber);
    }

    @Override
    public SysUser findFromUserID(String userId) {
        return mapper.findFromUserID(userId);
    }


    @Override
    public int backUser(String userId) {
        return mapper.backUser(userId);
    }

    @Override
    public String pass(String loginName) {
        return mapper.pass(loginName);
    }

    @Override
    public int roleId(String userId, String roleId) {
        return mapper.roleId(userId, roleId);
    }


    @Override
    public int updateStudentLeader(String userId,String leaderId) {
        mapper.deleteByUserId(userId);
        return mapper.insertStudentLeader(userId,leaderId);
    }

    @Override
    public List<UserDTO> findUserIdByRoleId(String roleId) {
        return mapper.findUserIdByRoleId(roleId);
    }
}